<template>
    <!-- 概览 -->
    <div class="home-overview">
      <!-- 用户信息 -->
      <div class="user-meta">
        <div class="avatar-box">
          <div class="avatar">
            <img :src="global.FILE_DOMAIN + userStore.profile.user.avatar_url" v-if="userStore.profile.user.avatar_url"/>
            <img src="@/assets/images/avatar.png" v-else>
            <div class="mask" @click="handlePreviewAvatar(global.FILE_DOMAIN + userStore.profile.user.avatar_url)" v-if="userStore.profile.user.avatar_url">点击预览</div>
          </div>
          <el-upload
            ref="uploadAvatarRef"
            class="avatar-uploader"
            :show-file-list="false"
            :on-change="handleAvatarChange"
            :auto-upload="false"
          >
            <template #trigger>
              <div class="edit-avatar-btn" @click="handleEditAvatar">编辑头像</div>
            </template>

          </el-upload>
        </div>

        <div class="username-box">
          <!-- 用户名区域 -->
          <div class="username-wrap">
            <i class="iconfont icon-yonghu"></i>
            <span>{{ userStore.profile.user.username }}</span>
          </div>

          <!-- 手机号区域 -->
          <div class="mobile-wrap">
            <i class="iconfont icon-shouji"></i>
            <span>{{ userStore.profile.user.mobile }}</span>
          </div>
        </div>
      </div>

      <!-- 操作区域 -->
      <div class="item">
        <!-- 安全设置 -->
        <a href="/user/set">
          <span class="iconfont icon-shezhi"></span>
          <p>安全设置</p>
        </a>

        <!-- 地址管理 -->
        <a href="/user/address">
          <span class="iconfont icon-address"></span>
          <p>地址管理</p>
        </a>

        <!-- 意见反馈 -->
        <a href="javascript:;">
          <span class="iconfont icon-yijianfankui"></span>
          <p>意见反馈</p>
        </a>
      </div>

      <!-- 图片预览 -->
      <el-dialog v-model="previewDialogVisible" width="50%" style="text-align: center;">
        <img style="width: 70%;" :src="previewDialogImageUrl" alt="">
      </el-dialog>

      <!-- 图片裁剪 -->
      <el-dialog v-model="cropperDialogVisible" :close-on-click-modal="false" width="50%" style="text-align: center;">
        <div class="cropper-wrap">
          <VueCropper
            ref="cropper"
            :img="option.img"
            :outputSize="option.outputSize"
            :outputType="option.outputType"
            :info="option.info"
            :full="option.full"
            :autoCropWidth="option.autoCropWidth"
            :autoCropHeight="option.autoCropHeight"
            :canMove="option.canMove"
            :canMoveBox="option.canMoveBox"
            :original="option.original"
            :autoCrop="option.autoCrop"
            :fixed="option.fixed"
            :fixedNumber="option.fixedNumber"
            :centerBox="option.centerBox"
            :infoTrue="option.infoTrue"
            :fixedBox="option.fixedBox"
            :high="option.high"
            :mode="option.mode"
          />
        </div>

        <div class="button-wrap">
          <el-button type="primary" @click="handleConfirmCropperAvatar">确定</el-button>
          <el-button type="primary" @click="handleRotateLeft">向左旋转</el-button>
          <el-button type="primary" @click="handleRotateRight">向右旋转</el-button>
          <el-button type="danger" @click="cropperDialogVisible = false">取消</el-button>
        </div>
      </el-dialog>


    </div>
</template>

<script setup>
import { inject, ref } from 'vue'
import { ElNotification, ElLoading } from 'element-plus'
import {useUserStore} from "@/stores/modules/user"
import { reqUploadImageAPI } from '@/apis/uploadFile'

// 导入图片裁剪插件
// https://github.com/xyxiao001/vue-cropper
// npm install vue-cropper@next
import 'vue-cropper/dist/index.css'
import { VueCropper }  from "vue-cropper"


// ---------------- 定义全局变量开始 ----------------
// 定义用户存储
const userStore = useUserStore()

// 获取全局对象
const global = inject('global')

// 定义预览图片
const previewDialogImageUrl = ref('')

// 定义图片预览弹层变量
const previewDialogVisible = ref(false)

// 定义图片裁剪弹层变量
const cropperDialogVisible = ref(false)

// 加载中实例
let loadingInstance = null
// ---------------- 定义全局变量结束 ----------------



// ---------------- 处理预览头像开始 ----------------
const handlePreviewAvatar = (url) => {
  previewDialogImageUrl.value = url;
  previewDialogVisible.value = true;
}
// ---------------- 处理预览头像结束 ----------------



// ---------------- 头像上传之前开始 ----------------
const handleAvatarChange = (uploadFile, uploadFiles) => {
  if (uploadFile.status == 'ready') {
    const isJPG = uploadFile.raw.type === 'image/jpeg';
    const isPNG = uploadFile.raw.type === 'image/png';
    const isLt2M = uploadFile.raw.size / 1024 / 1024 < 2;

    if (!isJPG && !isPNG) {
      ElNotification.err('上传头像图片只能是 JPG/PNG 格式!')
      return
    }
    if (!isLt2M) {
      ElNotification.err('上传头像图片大小不能超过 2MB!')
      return
    }

    let windowURL = window.URL || window.webkitURL;
    let dataURL = windowURL.createObjectURL(uploadFile.raw);

    cropperDialogVisible.value = true
    option.value.img = dataURL
  }
}
// ---------------- 头像上传之前结束 ----------------



// -------------- 头像裁剪、上传功能开始 -------------
// 裁剪组件
const cropper = ref()

// 裁剪参数
const option = ref({
	img: null, 			// 裁剪图片的地址
	outputSize: 1, 		//裁剪生成图片的质量(可选0.1 - 1)
	info: true, 		// 图片大小信息
	outputType: 'jpeg', // 裁剪生成图片的格式（jpeg || png || webp）
	canScale: false, 	// 图片是否允许滚轮缩放
	autoCrop: true, 	// 是否默认生成截图框
	autoCropWidth:  '100px', // 默认生成截图框宽度
	autoCropHeight: '100px', // 默认生成截图框高度
	high: true, // 是否按照设备的dpr 输出等比例图片
	fixedBox: false, // 固定截图框大小 不允许改变
	fixed: true, // 是否开启截图框宽高固定比例
	fixedNumber: [1, 1], // 截图框的宽高比例
	full: true, // 是否输出原图比例的截图
	canMoveBox: true, // 截图框能否拖动
	original: false, // 上传图片按照原始比例渲染
	centerBox: true, // 截图框是否被限制在图片里面
	infoTrue: false, // true 为展示真实输出图片宽高 false 展示看到的截图框宽高
	mode: '100% auto' // 图片默认渲染方式
})

// 处理确认裁剪头像
const handleConfirmCropperAvatar = () => {
	// 获取截图的blob数据
	cropper.value.getCropBlob(async (data)=>{
    // 加载提示框
    loadingInstance = ElLoading.service({
      text: '正在上传中',
      background: 'rgba(0, 0, 0, 0.8)'
    })

		const formData = new FormData()
		formData.append("file", data, 'xxx.jpg') // 必须要第三个参数，名称随便
		const {data:result} = await reqUploadImageAPI(formData)
		if (result.code != 200) {
      ElNotification({type: 'error', message: result.message})
			return false
		}

		// 更新头像
		await userStore.modifyAvatar({avatar_url: result.data.image_url})
    ElNotification({type: 'success',message: '修改成功'})
    loadingInstance.close()
		cropperDialogVisible.value = false
	})
}
// -------------- 头像裁剪、上传功能结束 -------------



// -------------- 向左、右旋转功能开始 ---------------
const handleRotateLeft = () => {
	cropper.value.rotateLeft()
}

const handleRotateRight = () => {
	cropper.value.rotateRight()
}
// -------------- 向左、右旋转功能结束 ---------------
</script>
  
<style scoped lang="scss">
  .home-overview {
    height: 132px;
    background: url(../../../assets/images/center-bg.png) no-repeat center / cover;
    display: flex;
    .user-meta {
      flex: 1;
      display: flex;
      align-items: center;
      .avatar-box {
        display: flex;
        align-items: center;
        flex-direction: column;
        margin-left: 10px;
        .avatar {
          width: 85px;
          height: 85px;
          border-radius: 50%;
          overflow: hidden;
          position: relative;
          cursor: pointer;
          &:hover .mask {
            opacity: 1;
          }

          img {
            width: 100%;
            height: 100%;
          }
          .mask {
            background-color: rgba(255, 255, 255, 0.4);
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            font-size: 13px;
            width: 85px;
            line-height: 85px;
            height: 85px;
            text-align: center;
            border-radius: 50%;
            color: $main-color;
            opacity: 0;
          }
        }
        .edit-avatar-btn {
          margin-top: 10px;
          background-color: #fff;
          padding: 5px 10px;
          cursor: pointer;
          border-radius: 2px;
          font-size: 12px;
          &:hover {
            color: $main-color;
          }
        }
      }
      .username-box {
        color: #fff;
        padding-left: 15px;
        .username-wrap {
          margin-bottom: 10px;
          .iconfont {
            padding-right: 5px;
          }
        }
        .mobile-wrap {
          .iconfont {
            padding-right: 5px;
          }
        }
      }
    }

    // 操作
    .item {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: space-around;
      &:first-child {
        border-right: 1px solid #f4f4f4;
      }
      a {
        color: white;
        font-size: 16px;
        text-align: center;
        .iconfont {
          font-size: 32px;
        }
        p {
          line-height: 32px;
        }
      }
    }

    .cropper-wrap {
      width: 400px;
      height:400px;
      display: flex;
      justify-content: center;
      background-color: orange;
      margin: 0 auto;
      margin-bottom: 10px;
    }
    .button-wrap {
      border-top: 1px solid #ccc;
      padding-top: 20px;
      display: flex;
      justify-content: space-evenly;
    }
  }
</style>